package admin.agent;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.CommonUtil;
import com.common.Const;
import com.common.MD5;
import com.common.ResultData;
import com.filter.BaseController;
import com.intercepter.AuthPassport;

import beans.sys.UserBean;
import beans.sys.UserModel;
import beans.user.user;
import service.sys.RoleUserServiceImpl;
import service.sys.UserServiceImpl;
import service.user.WXUserServiceImpl;
@Controller
@RequestMapping("/agentuser")
public class AgentUserController extends BaseController{
	@Resource
	private WXUserServiceImpl<user> wxUserServiceImpl;
	
	@Resource
	private RoleUserServiceImpl roleUserServiceImpl;

	@Resource
	private UserServiceImpl<UserBean> userServiceImpl;

	@RequestMapping("/index")
	public String Index(){
		return "admin/agentuser/user/index";
	}
	@RequestMapping("/GetPageListJson")
	@ResponseBody
	@AuthPassport(actionName="index",controllerName="agentuser")
	public void DataGrid(UserModel model,String condition,String keyword){
		ResultData<List<UserBean>> rd = new ResultData<>();
		try{
			rd.setPage(model.getPage());
			model.setType("2");
			if(!CommonUtil.isNullObject(condition)) {
				if(condition.equals("account"))model.setAccount(keyword);
				else if(condition.equals("username"))model.setUsername(keyword);
			}
			List<UserBean> m = userServiceImpl.queryByList(model);
			if(m.size()>0) {
				String[] ids = new String[m.size()];
				int i=0;
				for(UserBean u : m) {
					ids[i] = u.getWxopenid();
					i++;
				}
				List<user> userList = wxUserServiceImpl.getByIds(ids);
				for(UserBean u : m) {
					for(user temu : userList) {
						if(u.getWxopenid().equals(temu.getOpenid())) {
							u.setNickname(temu.getNickname());
							break;
						}
					}
				}
			}

			rd.setRows(m);
			rd.setRecords(model.getPager().getRowCount());
			rd.setTotal(model.getPager().getPageCount());
		}
		catch(Exception e){
			e.printStackTrace();
		}
		responseJson(rd);
	}

	@RequestMapping("/detail")
	@AuthPassport(actionName="index",controllerName="agentuser")
	public String Detail(String id){
		UserBean m = new UserBean();
		m.setIsuse("1");

		List<user> userList = wxUserServiceImpl.getAll();

		if(!CommonUtil.isNullObject(id)){
			try {
				m = (UserBean) userServiceImpl.queryById(id);
				user u = wxUserServiceImpl.queryById(m.getWxopenid());
				userList.add(u);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		getReq().setAttribute("userList", userList);
		getReq().setAttribute("agentuser", m);
		return "admin/agentuser/user/detail";
	}

	@RequestMapping("/save")
	@ResponseBody
	@AuthPassport(actionName="index",controllerName="agentuser")
	public void Save(UserBean ub){
		ResultData<String> rd = new ResultData<>();
		try {
			UserBean user = userServiceImpl.findByAccount(ub.getId(), ub.getAccount());
			if(user!=null){
				rd.setMessage("当前工号已经存在!");
				responseJson(rd);
				return;
			}

			if(CommonUtil.isNullObject(ub.getId())){//新增
				UserBean u = (UserBean)getReq().getSession().getAttribute("user");

				String id = UUID.randomUUID().toString();
				ub.setType("2");
				ub.setId(id);
				ub.setAddtime(new Date());
				ub.setPassword(MD5.MD5Encode(ub.getPassword()));
				ub.setAdduser(u.getId());
				ub.setIsuse("1");
				userServiceImpl.add(ub);
				
				roleUserServiceImpl.saveRoleFun(Const.dlRole, id);
			}
			else{//编辑
				userServiceImpl.update(ub);
			}
			rd.setSuccess(true);
		} catch (Exception e) {
			rd.setType(3);
			rd.setMessage("系统错误,请联系管理员!");
			e.printStackTrace();
		}
		rd.setType(1);
		rd.setMessage("保存成功!");
		responseJson(rd);
	}

	@RequestMapping("/ExistAccount")
	@ResponseBody
	public void ExistAccount(String account,String keyValue) {
		UserBean user = userServiceImpl.findByAccount(keyValue, account);
		if(user!=null){
			responseText("false");
		}
		else
			responseText("true");
	}

	@RequestMapping("/del")
	@ResponseBody
	@AuthPassport(actionName="index",controllerName="agentuser")
	public void Del(){
		String[] ids = getReq().getParameterValues("ids");
		try{
			userServiceImpl.deletes(ids);
		}
		catch(Exception e){
			e.printStackTrace();
		}
		ResultData<String> rd = new ResultData<>();
		rd.setType(1);
		rd.setMessage("删除成功");
		responseJson(rd);
	}

	@RequestMapping("/editpwd")
	@ResponseBody
	public void EditPwd(String id){
		try{
			userServiceImpl.initPwd(id, MD5.MD5Encode("11111"));
		}
		catch(Exception e){
			e.printStackTrace();
		}
		ResultData<String> rd = new ResultData<>();
		rd.setType(1);
		rd.setMessage("初始化成功");
		responseJson(rd);
	}

	@RequestMapping("/EnabledAccount")
	@ResponseBody
	@AuthPassport(actionName="index",controllerName="agentuser")
	public void EnabledAccount(String id,String type){
		try{
			userServiceImpl.UseStatus(id, type);
		}
		catch(Exception e){
			e.printStackTrace();
		}
		ResultData<String> rd = new ResultData<>();
		rd.setType(1);
		rd.setMessage("状态更改成功");
		responseJson(rd);
	}

	@RequestMapping("/editpwdindex")
	public String EditPwdIndex() {
		return "admin/agentuser/user/editpwdindex";
	}
}
